﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using VOL;
using DAL;

namespace BLL
{
    public class ThanhPhoBL
    {
        private DataAccess dataAccess;
        public ThanhPhoBL()
        {
            dataAccess = new DataAccess();
        }

        public DataTable GetData()
        {
            string strSql = "SELECT * FROM tbl_ThanhPho ORDER BY MaThanhPho DESC ";
            return dataAccess.GetData(strSql);
        }

        //Thêm
        public bool insertThanhPho(ThanhPho obj)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@TenThanhPho", SqlDbType.NVarChar) { Value = obj.TenThanhPho };
            param[1] = new SqlParameter("@MaVungDienThoai", SqlDbType.NVarChar) { Value = obj.MaVungDienThoai };

            string strSql = "INSERT INTO tbl_ThanhPho(TenThanhPho,MaVungDienThoai) VALUES (@TenThanhPho,@MaVungDienThoai)";
            dataAccess.Execute(strSql, param);

            return true;
        }

        ////Xóa
        public bool deleteThanhPho(ThanhPho obj)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaThanhPho", SqlDbType.Int) { Value = obj.MaThanhPho };

            string strSql = "DELETE FROM tbl_ThanhPho WHERE MaThanhPho = @MaThanhPho";
            dataAccess.Execute(strSql, param);
            return true;
        }

        ////Sửa
        public bool updateThanhPho(ThanhPho obj)
        {
            SqlParameter[] param = new SqlParameter[3];
            param[0] = new SqlParameter("@TenThanhPho", SqlDbType.NVarChar) { Value = obj.TenThanhPho };
            param[1] = new SqlParameter("@MaThanhPho", SqlDbType.Int) { Value = obj.MaThanhPho };
            param[2] = new SqlParameter("@MaVungDienThoai", SqlDbType.NVarChar) { Value = obj.MaVungDienThoai };

            string strSql = "UPDATE tbl_ThanhPho SET " +
                            "TenThanhPho = @TenThanhPho, " +
                            "MaVungDienThoai = @MaVungDienThoai " +
                            "WHERE MaThanhPho = @MaThanhPho";
            dataAccess.Execute(strSql, param);
            return true;
        }

        ////Tìm theo Mã Thành Phố
        public DataTable searchIDThanhPho(int maThanhPho)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaThanhPho", SqlDbType.Int) { Value = maThanhPho };

            string strSql = "SELECT * FROM tbl_ThanhPho WHERE MaThanhPho = @MaThanhPho";
            return dataAccess.GetData(strSql, param);
        }
    }
}
